package com.example.demo;

import java.net.*;
import java.io.*;

public class Main {
    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(8002);
            System.out.println("running at 127.0.0.1:8002");
            Socket server;
            while (true) {
                try
                {
                    server = serverSocket.accept();
                    BufferedReader in = new BufferedReader(new InputStreamReader(server.getInputStream()));
                    System.out.println(in.readLine());
                    OutputStream out = server.getOutputStream();
                    out.write("HTTP/1.1 200 OK\r\ncontent-type: text/html; charset=utf-8\r\n\r\nhello world".getBytes());
                    server.close();
                }catch(SocketTimeoutException s)
                {
                    System.out.println("Socket timed out!");
                    break;
                }catch(IOException e)
                {
                    e.printStackTrace();
                    break;
                }

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
